package org.jadapter.tests.adapters;

import org.jadapter.annotations.Adapter;
import org.jadapter.tests.examples.Csv;

import java.util.List;
import java.util.Map;

/**
 * User: pjesi
 * Date: Apr 12, 2009
 * Time: 2:12:13 PM
 */
@Adapter(to = Csv.class, from = Map.class)
public class AnnotationAdapter implements Csv {


    private Map<?,?> map;


    public AnnotationAdapter(List<?> list) {
        // confusing the reflection algorithm
    }

    public AnnotationAdapter(Map<?,?> map) {
        this.map = map;
    }

    public String csv() {
        if(map == null) {
            return "";
        }
        StringBuilder builder = new StringBuilder();
        for(Object key : map.keySet()) {
            builder.append(key).append(':').append(map.get(key)).append(',');
        }
        if(map.size() > 0) {
            builder.deleteCharAt(builder.length() - 1);
        }
        return builder.toString();
    }
}
